package elitech.vietnam.myfashion.extend;

import java.io.IOException;
import java.util.List;

import org.xmlpull.v1.XmlPullParserException;

import android.os.AsyncTask;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.koushikdutta.urlimageviewhelper.UrlImageViewHelper;

import elitech.vietnam.myfashion.MainActivity;
import elitech.vietnam.myfashion.R;
import elitech.vietnam.myfashion.config.Const;
import elitech.vietnam.myfashion.doinbackground.CallServiceTask;
import elitech.vietnam.myfashion.entites.Member;
import elitech.vietnam.myfashion.fragment.MemberStyleFragment;

public class ListFollowerArrayAdapter extends ArrayAdapter<Member> {
	private MainActivity mContext;
	private ListFollowerArrayAdapter mAdapter;
	private boolean isFollowing = false;
	
	
	public ListFollowerArrayAdapter(MainActivity context, int resource,
			List<Member> objects, boolean isFollowing) {
		super(context, resource, objects);
		this.isFollowing = isFollowing;
		mContext = context;
		mAdapter = this;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View row = convertView;
		ViewHolder holder;
		final Member i = getItem(position);

		if (row == null) {
			LayoutInflater inflater = LayoutInflater.from(getContext());
			row = inflater.inflate(R.layout.followlistitem, parent, false);

			holder = new ViewHolder();
			holder.mAvatar = (ImageView) row
					.findViewById(R.id.followitem_image);
			holder.mName = (TextView) row.findViewById(R.id.followitem_name);
			holder.mProfile = (TextView) row
					.findViewById(R.id.followitem_profile);
			holder.mBtnUnfollow = (Button) row
					.findViewById(R.id.followitem_btnUnfollow);
			row.setTag(holder);
		} else
			holder = (ViewHolder) row.getTag();

		UrlImageViewHelper.setUrlDrawable(holder.mAvatar, Const.SERVER_IMAGE_THUMB_URL + i.Image, R.drawable.no_avatar);
		holder.mName.setText(i.Name);
		holder.mProfile.setText(i.Profile);
		if (isFollowing) {
			holder.mBtnUnfollow.setVisibility(View.VISIBLE);
			holder.mBtnUnfollow.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					new CallServiceTask() {
						int rs = 0;

						public void work() {
							try {
								rs = service.unFollow(mContext.mUser.Id, i.Id);
							} catch (IOException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							} catch (XmlPullParserException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						};

						protected void onPostExecute(Void result) {
							if (rs == 1) {
								mAdapter.remove(i);
								notifyDataSetChanged();
							} else {

							}
						};
					}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
				}
			});
			
			row.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					if (mContext.mMemberStyleFragment == null)
						mContext.mMemberStyleFragment = new MemberStyleFragment();
					if (!mContext.mMemberStyleFragment.isAdded()) {
						mContext.mMemberStyleFragment.setMember(i);
						mContext.replaceFragment(mContext.mMemberStyleFragment, true);
//						mContext.changeTitle(mContext.getString(R.string.styler), false);
					}
				}
			});
		}

		return row;
	}

	static class ViewHolder {
		ImageView mAvatar;
		TextView mName;
		TextView mProfile;
		Button mBtnUnfollow;
	}
}
